package com.saudidentalapp;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.saudiapp.lazylist.ImageLoader;
import com.saudidentalapp.datamodels.Speaker;

public class SpeakerAdapter extends ArrayAdapter<Speaker>
{

	Context context;
    int layoutResourceId;    
    ArrayList<Speaker>data;
    
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    ContactInformationListener contactInformationListener;
    
	public SpeakerAdapter(Context context, int resource, List<Speaker> objects) 
	{
		super(context, resource, objects);
		 this.layoutResourceId = resource;
	        this.context = context;
	        this.data = (ArrayList<Speaker>) objects;	        
	        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        imageLoader=new ImageLoader(context.getApplicationContext());
	}
	
	
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	{
		View row = convertView;
		final SpeakerHolder holder;
       
        if(row == null)
        {
           row = inflater.inflate(layoutResourceId, parent, false);
           
            holder = new SpeakerHolder();
            holder.txtName = (TextView)row.findViewById(R.id.xtxtSpeakerName);
            holder.txtLocation = (TextView)row.findViewById(R.id.xtxtSpeakerLocation);
            holder.imgThumb = (ImageView)row.findViewById(R.id.ximgThumb);
            holder.imgContact = (ImageView)row.findViewById(R.id.ximgContact);
            holder.imgEmail = (ImageView)row.findViewById(R.id.ximgEmail);         
           
            row.setTag(holder);
        }
        else
        {
            holder = (SpeakerHolder)row.getTag();
        }
       
        Speaker speaker = getItem(position);
        holder.txtName.setText(speaker.getName());
        holder.txtLocation.setText(speaker.getLocation()); 
        imageLoader.DisplayImage(speaker.getThumbImage(), holder.imgThumb);
       
        if(!speaker.getContactNo().equalsIgnoreCase("#") &&!speaker.getContactNo().equalsIgnoreCase(""))
        	{
        		holder.imgContact.setTag(speaker.getContactNo());
        		holder.imgContact.setVisibility(View.VISIBLE);
        		holder.imgContact.setTag(speaker.getContactNo());
        	}
	        else
	        {
	        	holder.imgContact.setVisibility(View.GONE);
	        }
        
        holder.imgContact.setOnClickListener(new OnClickListener() 
        {
			
			@Override
			public void onClick(View v) 
			{
				contactInformationListener.OnCallClicked(holder.imgContact.getTag().toString(), position);
				
			}
		});
        
        if(!speaker.getEmail().equalsIgnoreCase("#")&&!speaker.getEmail().equalsIgnoreCase(""))
    	{
        	holder.imgEmail.setTag(speaker.getEmail());
        	holder.imgEmail.setTag(speaker.getEmail());
        	holder.imgEmail.setVisibility(View.VISIBLE);
    	}
        else
        {
        	holder.imgEmail.setVisibility(View.GONE);
        }
        
        
        holder.imgEmail.setOnClickListener(new OnClickListener() 
        {
			
			@Override
			public void onClick(View v) 
			{
				contactInformationListener.OnEmailClicked( holder.imgEmail.getTag().toString(), position);
				
			}
		});
               
        return row;
    }
   
    static class SpeakerHolder
    {       
    	ImageView imgThumb;
    	TextView txtName;
    	TextView txtLocation;
    	ImageView imgContact;
    	ImageView imgEmail;      
    }
    public void setContactInformationListener(
			ContactInformationListener contactInformationListener) {
		this.contactInformationListener = contactInformationListener;
	}
    
    
    public interface ContactInformationListener
	{
		public abstract void OnEmailClicked(String Email,int position);
		public abstract void OnCallClicked(String number,int position);
	}
}
